//
//  titleView.m
//  评论
//
//  Created by dllo on 15/7/11.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "BaseHeader.h"
#import "titleView.h"
#import "commentTableViewCell.h"

@interface titleView ()
@property (nonatomic, assign) NSInteger seleCount;

@end
@implementation titleView

- (void)dealloc
{
    [_seg release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.seg = [[UISegmentedControl alloc] initWithItems:@[@"最新评论", @"最热评论"]];
        self.seg = [[[UISegmentedControl alloc] initWithItems:@[@"最新评论", @"最热评论"]] autorelease];
        [self addSubview:self.seg];
        [self.seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
        [self createNotificationCenter];
    }
    return self;
}

- (void)layoutSubviews
{
    self.seg.frame = CGRectMake(0, 0, 200 * SCREEN_WIDTH / 375, 30 * SCREEN_HEIGHT / 667);
    self.seg.selectedSegmentIndex = 0;
    self.seg.tintColor = [UIColor grayColor];
    self.seg.alpha = 1;
}

+ (instancetype)titleViewWithFrame:(CGRect)frame
{
    id result = [[[self alloc] initWithFrame:frame] autorelease];
    return result;
}

#pragma mark - 注册通知中心
- (void)createNotificationCenter
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(changeSele:) name:@"count" object:nil];
}
#pragma mark - 实现通知中心方法
- (void)changeSele:(NSNotification *)noti
{
    self.seleCount = [noti.object integerValue];
    //通知中心传过来的值与UISegmentedControl的选中效果对应
    self.seg.selectedSegmentIndex = self.seleCount;
}

- (void)change:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        //滚动效果
        [self.scro setContentOffset:CGPointMake(0, 0) animated:YES];
    } else {
        [self.scro setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
